module utils {
/**
 * @Date   2016/5/18
 * @author speed
 * win 管理工具
 */
	export class WinUtil {
        private static sceneCatchDic:any = {};
        private static sceneCatchArr:Array<any> = []; 
        private static maxCacheScene: number = 8;       //场景的缓存数量
        private static minCacheScene: number = 8;
        
        private static sceneRes(cls):any{
            return null;
        }
        
        /**
        * 不释放的场景
        */
        private static unReleaseScene: any = [];
        
        /**
           * 打开场景
           * @param cls 可以是string，也可以是class
           * @param param 
        */
        public static showScene(cls: any,param?:any):any{
            var classNameKey:string;
            var classDefinition:any;
            var classPath: string;
            var classPathArr:Array<string>;
            if(!cls) {
                console.error(cls +"is null");
                return null;
            }
            else if((typeof cls) == "string") {
                classPathArr = cls.split(".");
                classNameKey = classPathArr[classPathArr.length - 1];
                classDefinition = egret.getDefinitionByName(cls);
                if(!classDefinition) {
                    console.error(cls +"的getDefinitionByName为null");
                    return null;
                }
            }
            else {
                classDefinition = cls;
                classPath = egret.getQualifiedClassName(cls);
                classPathArr = classPath.split(".");
                classNameKey = classPathArr[classPathArr.length - 1];
            }
            var scene = WinUtil.sceneCatchDic[classNameKey] || new cls();
            WinUtil.addScene(scene,param);
            return scene;
        }
        
        private static addScene(scene,param?:any):any{
            if(scene){
                if(param && scene.setData){
                    scene.setData(param);
                }
                if(WinUtil.sceneCatchArr.length > WinUtil.maxCacheScene) {
                    var className = WinUtil.sceneCatchArr.shift();
                    WinUtil.disposeWinByName(className);
                }
                WinUtil.addSceneToCatch(scene);                
           }
            return scene;
        }
        
        //把场景添加到缓存
        private static addSceneToCatch(scane):void{
            var classPath = egret.getQualifiedClassName(scane);
            var arr = classPath.split(".");
            var className = arr[arr.length-1];
            var index = WinUtil.sceneCatchArr.indexOf(className);
            if(index >= 0) {
                WinUtil.sceneCatchArr.splice(index,1);
            }
            WinUtil.sceneCatchDic[className] = scane;
            WinUtil.sceneCatchArr.push(className);
        }
        
        /**
         * 检查是否不释放的场景
         * @param scene
         * @returns {boolean}
         */
        private static isUnReleaseScene(clsName: string): boolean {
            return WinUtil.unReleaseScene.indexOf(clsName) > -1;
        }
        
        private static disposeWinByName(clsName?): void {
            var self = WinUtil;
            if(WinUtil.isUnReleaseScene(clsName)) {
                LogManager.log("isUnreleaseScene:"+clsName);
                return;
            }
            if(!clsName) {
                if(self.sceneCatchArr.length > self.minCacheScene) {
                    clsName = self.sceneCatchArr.shift();
                }
            }
            if(clsName) {
                    var scene = self.sceneCatchDic[clsName];
                    if(scene) {
                        if(scene.parent) {
                            scene.parent.removeChild(scene);
                        }
                        self.sceneCatchDic[clsName] = null;
                        delete self.sceneCatchDic[clsName];
                        var res = self.sceneRes[clsName];
                        if(res) {
                            for(var k in res) {
                                var g = res[k];
                                if(g != "common" && g != "preload") {
                                    RES.destroyRes(g,false);
                                }
                            }
                        }
                        LogManager.log("释放窗口/场景："+clsName);
                    }
            }
        }
        
        private static getClassNameByCls(cls):string{
            var classPathArr:Array<string>;
            if(!cls){
                return null;
            } else if((typeof cls) == "string"){
                classPathArr = cls.split(".");         
            }else{
                classPathArr = egret.getQualifiedClassName(cls).split(".");
            }
            return classPathArr[classPathArr.length - 1];
        }
    
        public static loadResource(cls,callBack: Function,processFun?:Function): any {
            var classNameKey: string = WinUtil.getClassNameByCls(cls);
            var loadArr = WinUtil.sceneRes(classNameKey);
            if(loadArr && loadArr.length > 0) {
                GroupLoad.instance.loadGroupList(loadArr,function() {
                    if(callBack) {
                        callBack();
                    }
                },processFun);
            }
            else {
                if(callBack) {
                    callBack();
                }
            }  
        }
	}
}
